package com.huicoo.forestmanager.base;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.others.Constants;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.SharedPreferenceUtil;
import com.huicoo.forestmanager.util.toast.ToastUtils;
import com.umeng.commonsdk.UMConfigure;

import java.io.File;

public class BaseApplication extends Application {
    public static final String TAG = "Init";
    private static BaseApplication mApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        ToastUtils.init(this);
        LogUtils.isDebug = true;

        //友盟init
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_BOX, null);
        UMConfigure.setLogEnabled(true);

        clearWebCache();

        initCloudChannel(this);
    }

    public static BaseApplication getMyApplication() {
        return mApplication;
    }

    // 清除web缓存
    public static void clearWebCache() {
        long lastTime = SharedPreferenceUtil.LoadLongData(Constants.PROJECT_NAME, Constants.WEB_CACHE_TIME);
        long nowTime = System.currentTimeMillis();
        LogUtils.v("gogogo time = " + nowTime + "  --  " + lastTime);
        if (nowTime - lastTime >= 30 * 60 * 1000) {
            clearWebViewCache();
            SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.WEB_CACHE_TIME, nowTime);
        }
    }

    /**
     * 清除WebView缓存
     */
    public static void clearWebViewCache() {
        File file = mApplication.getCacheDir().getAbsoluteFile();
        deleteFile(file);
    }

    /**
     * 递归删除 文件/文件夹
     */
    public static void deleteFile(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    deleteFile(files[i]);
                }
            }
            file.delete();
        }
    }

    /**
     * 初始化云推送通道
     *
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        //设置通知大图标
        pushService.setNotificationLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

        //设置通知小图标
        pushService.setNotificationSmallIcon(R.mipmap.ic_launcher);
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "init cloudchannel success");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // 通知渠道的id
            String id = "1";
            // 用户可以看到的通知渠道的名字.
            CharSequence name = "notification channel";
            // 用户可以看到的通知渠道的描述
            String description = "notification description";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            // 配置通知渠道的属性
            mChannel.setDescription(description);
            // 设置通知出现时的闪灯（如果 android 设备支持的话）
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            // 设置通知出现时的震动（如果 android 设备支持的话）
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            //最后在notificationmanager中创建该通知渠道
            mNotificationManager.createNotificationChannel(mChannel);
        }


    }

}
